# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import re

import cpca
import scrapy

from components.custom_item import UpdateCustomItem
from utils.tools import TimeMixIn


class NetTaxIllegalProItem(UpdateCustomItem):
    """
    重大税务违法信息
    """

    __update_key__ = [
        "company_name",
        "address",
        "taxpayer_id",
        "org_code",
        "legal_representative",
        "illegal_legal_representative",
        "resp_financial",
        "resp_financial_head",
        "resp_person",
        "resp_intermediary",
        "illegal_status",
        "illegal_facts",
        "basis_and_punishment",
    ]

    __table_name__ = "net_tax_illegal"

    company_name = scrapy.Field()  # 公司名称
    address = scrapy.Field()  # 注册地址
    taxpayer_id = scrapy.Field()  # 纳税人识别号
    org_code = scrapy.Field()  # 组织机构代码
    legal_representative = scrapy.Field()  # 法定代表人或者负责人信息
    illegal_legal_representative = scrapy.Field()  # 违法期间法人代表或者负责人信息
    resp_financial = scrapy.Field()  # 负有直接责任的财务人员信息
    resp_financial_head = scrapy.Field()  # 负有直接责任的财务负责人信息
    resp_person = scrapy.Field()  # 实际负责人信息
    resp_intermediary = scrapy.Field()  # 负有直接责任的中介机构信息
    illegal_status = scrapy.Field()  # 违法性质
    illegal_facts = scrapy.Field()  # 违法事实
    basis_and_punishment = scrapy.Field()  # 相关法律依据及税务处理税务行政处罚情况
    province = scrapy.Field()  # 省份
    city = scrapy.Field()  # 市
    country = scrapy.Field()  # 区县
    year = scrapy.Field()  # 年份

    def pre_to_db(self):
        """
        入库前的处理
        """
        self.address = self.process_str(self.address)
        self.taxpayer_id = self.process_str(self.taxpayer_id)
        self.org_code = self.process_str(self.org_code)
        self.legal_representative = self.process_str(self.legal_representative)
        self.illegal_legal_representative = self.process_str(self.illegal_legal_representative)
        self.resp_financial = self.process_str(self.resp_financial)
        self.resp_financial_head = self.process_str(self.resp_financial_head)
        self.resp_person = self.process_str(self.resp_person)
        self.resp_intermediary = self.process_str(self.resp_intermediary)
        self.illegal_status = self.process_str(self.illegal_status)

        illegal_facts = self.process_str(self.illegal_facts)
        if illegal_facts:
            self.illegal_facts = {"content": illegal_facts}
        else:
            self.illegal_facts = None

        basis_and_punishment = self.process_str(self.basis_and_punishment)
        if basis_and_punishment:
            self.basis_and_punishment = {"content": basis_and_punishment}
        else:
            self.basis_and_punishment = None

        self.country = self.process_str(self.country)
        self.city = self.process_str(self.city)
        self.year = self.process_str(str(self.year))

    @staticmethod
    def process_str(value):
        """
        处理字符串
        :param value:
        :return:
        """
        if value is None:
            return ""
        if value == ",,,," or value == ",,,":
            return ""
        return re.sub(r"\s+", " ", value)

    @staticmethod
    def process_year(value: str):
        date_ = TimeMixIn.format_date(value)
        if date_:
            return date_.year
        return ""

    @staticmethod
    def resolve_register_address(register_address):
        if register_address:
            df = cpca.transform([register_address])
            if df.iloc[0, 1] == "市辖区":
                df.iloc[0, 1] = df.iloc[0, 0]
            data = {"province": df.iloc[0, 0], "city": df.iloc[0, 1], "area": df.iloc[0, 2]}

            return data["city"], data["area"]
        else:
            return "", ""
